package com.java.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.java.system.entity.Menu;
import com.java.system.mapper.MenuMapper;
import com.java.system.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-21
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    MenuMapper menuMapper;
    @Override
    public List<Menu> getAllMenu() {
        LambdaQueryWrapper<Menu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getPid, 0);
        List<Menu> menuList = menuMapper.selectList(queryWrapper);
        if(!StringUtils.isEmpty(menuList)){
            for(Menu menu:menuList){
                LambdaQueryWrapper<Menu> subWrapper=new LambdaQueryWrapper<>();
                subWrapper.eq(Menu::getPid,menu.getMenuId());
                List<Menu> subMenuList = menuMapper.selectList(subWrapper);
                menu.setChild(subMenuList);
            }
        }
        return menuList;
    }

    @Override
    public List<Menu> getMenuListByUserId(Integer userId) {
        List<Menu> menuListByUserId = menuMapper.getMenuListByUserId(userId, 0);
        if(!StringUtils.isEmpty(menuListByUserId)){
            for(Menu menu:menuListByUserId){
                List<Menu> menuList = menuMapper.getMenuListByUserId(userId, menu.getMenuId());
                menu.setChild(menuList);
            }
        }

        return menuListByUserId;
    }
}
